﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using coffeeshop.coffeeDataSetTableAdapters;

namespace coffeeshop.Models
{
    public static class  Config
    {
        private static ConfigTableAdapter configAdapter = new ConfigTableAdapter();
        private const string keyLastLoggedInUserName = "LastLoggedInUserName";
        private const string keyPromotionAmount = "PromotionAmount";
        private const string keyOrderIndex = "OrderIndex_{0}";
        private const string keyNumberOfTables = "NumberOfTables";
        private const string keyShopName = "ShopName";
        private const string keyShopLogo = "ShopLogo";
        private const string keyThankYouText = "ThankYouText";

        public static string ShopName
        {
            get 
            {
                coffeeDataSet.ConfigDataTable result = configAdapter.GetConfigByName(keyShopName);
                if (result.Rows.Count > 0)
                {
                    return result.Rows[0]["Value"].ToString();
                }

                return string.Empty;
            }
            set {
                configAdapter.InsertUpdateConfigKey(keyShopName, value); 
            }
        }

        public static string ShopLogo
        {
            get {
                coffeeDataSet.ConfigDataTable result = configAdapter.GetConfigByName(keyShopLogo);
                if (result.Rows.Count > 0)
                {
                    return result.Rows[0]["Value"].ToString();
                }

                return string.Empty;
            }
            set {
                configAdapter.InsertUpdateConfigKey(keyShopLogo, value); 
            }
        }

        public static string ThankYouText
        {
            get
            {
                coffeeDataSet.ConfigDataTable result = configAdapter.GetConfigByName(keyThankYouText);
                if (result.Rows.Count > 0)
                {
                    return result.Rows[0]["Value"].ToString();
                }

                return string.Empty;
            }
            set
            {
                configAdapter.InsertUpdateConfigKey(keyThankYouText, value);
            }
        }

        public static string GetLastLoggedInUsername()
        {
            coffeeDataSet.ConfigDataTable result = configAdapter.GetConfigByName(keyLastLoggedInUserName);
            if (result.Rows.Count > 0)
            {
                return result.Rows[0]["Value"].ToString();
            }

            return string.Empty;
        }

        public static void SaveLoggedInUsername(string userName)
        {
            configAdapter.InsertUpdateConfigKey(keyLastLoggedInUserName, userName); 
        }

        public static int GetNumberOfTables()
        {
            coffeeDataSet.ConfigDataTable result = configAdapter.GetConfigByName(keyNumberOfTables);
            if (result.Rows.Count > 0)
            {
                int _number;
                int.TryParse(result.Rows[0]["Value"].ToString(), out _number);
                if (_number > 0)
                    return _number;
            }

            // else return all the tables in db
            return OrderTable.CountActiveTable().Count;
        }

        public static void SetNumberOfTables(int _number)
        {
            configAdapter.InsertUpdateConfigKey(keyNumberOfTables, _number.ToString()); 
        }

        public static float GetPromotionAmount()
        {
            coffeeDataSet.ConfigDataTable result = configAdapter.GetConfigByName(keyPromotionAmount);
            if (result.Rows.Count > 0)
            {
                float _number;
                float.TryParse(result.Rows[0]["Value"].ToString(), out _number);
                if (_number > 0)
                    return _number;
            }

            return 0;
        }

        public static void SetPromotionAmount(float _amount)
        {
            configAdapter.InsertUpdateConfigKey(keyPromotionAmount, _amount.ToString());
        }
    }
}
